package com.betterjr.modules.risk.loan.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.betterjr.common.service.BaseService;
import com.betterjr.common.utils.BTAssert;
import com.betterjr.common.utils.Collections3;
import com.betterjr.common.utils.QueryTermBuilder;
import com.betterjr.modules.account.entity.CustInfo;
import com.betterjr.modules.account.service.CustAccountService;
import com.betterjr.modules.risk.loan.dao.LoanProjectChargeMapper;
import com.betterjr.modules.risk.loan.dao.LoanProjectSynchInterfaces;
import com.betterjr.modules.risk.loan.entity.LoanProjectCharge;
import com.betterjr.modules.risk.loan.entity.LoanProjectRelation;
import com.betterjr.modules.risk.loanmanage.entity.Partnership;
import com.betterjr.modules.risk.loanmanage.service.PartnershipService;

@Service
public class LoanProjectChargeService extends BaseService<LoanProjectChargeMapper, LoanProjectCharge>
        implements LoanProjectSynchInterfaces {

    @Autowired
    private CustAccountService custAccountService;

    @Autowired
    private PartnershipService partnerShipService;

    @Override
    public void InvokeDeleted(final LoanProjectRelation anLoan) {

        loggerin.debug(" LoanProjectChargeService  InvokeDeleted " + anLoan);

    }

    @Override
    public void InvokeEffective(final LoanProjectRelation anLoan) {

        logger.debug(" LoanProjectChargeService  InvokeEffective " + anLoan);
        //

        try {
            final LoanProjectCharge charge = new LoanProjectCharge();

            charge.saveAddInitValue();

            convertChargeByLoan(anLoan, charge);

            saveCharge(charge);

        }
        catch (Exception e) {
            BTAssert.notNull(null, "创建服务费异常" + e.getMessage());
        }
    }

    private void convertChargeByLoan(final LoanProjectRelation anLoan, final LoanProjectCharge charge) {

        charge.setCoreName(anLoan.getCoreName());
        charge.setCoreNo(anLoan.getCoreNo());
        charge.setCustCode(anLoan.getCustCode());
        charge.setCustName(anLoan.getCustName());
        charge.setCustNo(anLoan.getCustNo());
        charge.setFactoryName(anLoan.getFactoryName());
        charge.setFactoryNo(anLoan.getFactoryNo());
        charge.setFinancingMaturityDate(anLoan.getFinancingMaturityDate());
        charge.setLoanBalance(anLoan.getLoanBalance());
        charge.setLoanDate(anLoan.getLoanDate());
        charge.setServiceRate(anLoan.getServiceRate());
        // 贷款金额*费率/360*融资期限 或者 贷款金额*费率*融资期限/360
        charge.calculateCharge();

        charge.setLoanId(anLoan.getId());
        // 封装分润比例
        final List<Partnership> list = partnerShipService.selectByProperty("coreCompanyId", charge.getCoreNo());
        BTAssert.isTrue(!Collections3.isEmpty(list), "请配置企业关系");
        charge.setSettleAccountsType(Collections3.getFirst(list).getPaymentMode());
        charge.setShareScale(Collections3.getFirst(list).getProfitRatio());

        // 封装核心企业组织机构代码
        final CustInfo info = custAccountService.findCustInfo(anLoan.getCoreNo());
        BTAssert.notNull(info, "查询核心企业失败");
        charge.setCoreOperorg(info.getOperOrg());

        // 封装分润比例和结算周期

    }

    @SuppressWarnings("unchecked")
    public LoanProjectCharge saveCharge(final LoanProjectCharge anCharge) {

        final Map<String, Object> map = QueryTermBuilder.newInstance().put("loanId", anCharge.getLoanId()).build();

        map.put("businStatus", new String[] { "1", "2" });

        final List<LoanProjectCharge> list = this.selectByProperty(map);

        if (!Collections3.isEmpty(list)) {
            for (final LoanProjectCharge loanProjectCharge : list) {
                if (loanProjectCharge.getBusinStatus().equals("2")) {
                    BTAssert.notNull(null, "当前贷款项目已经收取了服务费");
                }
                loanProjectCharge.setBusinStatus("3");
                this.updateByPrimaryKeySelective(loanProjectCharge);
            }
        }

        this.insertSelective(anCharge);

        return anCharge;

    }

}
